package com.zz.nondatain.view.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zz.nondatain.R;
import com.zz.nondatain.base.BaseActivity;
import com.zz.nondatain.databinding.ActivityAnnouncementBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 通知公告页面，显示系统公告列表
 */
public class AnnouncementActivity extends BaseActivity<ActivityAnnouncementBinding> {

    private List<AnnouncementItem> announcements = new ArrayList<>();
    private AnnouncementAdapter adapter;
    private List<AnnouncementItem> filteredAnnouncements = new ArrayList<>();

    @Override
    protected void initView() {
        // 设置RecyclerView
        binding.recyclerAnnouncements.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AnnouncementAdapter(filteredAnnouncements);
        binding.recyclerAnnouncements.setAdapter(adapter);
        
        // 加载公告数据
        loadAnnouncements();
    }

    @Override
    protected void initData() {
        // 初始化数据在loadAnnouncements中完成
    }

    @Override
    protected void initObservers() {
        setupListeners();
    }

    @Override
    protected ActivityAnnouncementBinding getViewBinding() {
        return ActivityAnnouncementBinding.inflate(getLayoutInflater());
    }
    
    /**
     * 设置监听器
     */
    private void setupListeners() {
        // 返回按钮点击事件
        binding.btnBack.setOnClickListener(v -> finish());
        
        // 搜索框文本变化监听
        binding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                filterAnnouncements(s.toString());
            }
        });
        
        // 筛选按钮点击事件
        binding.btnFilter.setOnClickListener(v -> {
            // 这里可以实现筛选功能，比如按时间排序等
            // 为简化起见，仅显示一个提示
            showToast("筛选功能");
        });
    }
    
    /**
     * 加载公告数据
     */
    private void loadAnnouncements() {
        // 清空现有记录
        announcements.clear();
        
        // 添加模拟数据 - 实际应该从API或数据库获取
        announcements.add(new AnnouncementItem(
                "123123123", 
                "非羁押人员管控",
                "2023-08-08 18:03"));
                
        announcements.add(new AnnouncementItem(
                "测试公告", 
                "非羁押人员管控",
                "2023-07-21 14:28"));
        
        // 初始时显示所有公告
        filteredAnnouncements.clear();
        filteredAnnouncements.addAll(announcements);
        
        // 刷新适配器
        adapter.notifyDataSetChanged();
        
        // 如果有内容，显示底部提示
        if (!announcements.isEmpty()) {
            binding.tvEndHint.setVisibility(View.VISIBLE);
        } else {
            binding.tvEndHint.setVisibility(View.GONE);
        }
    }
    
    /**
     * 过滤公告数据
     */
    private void filterAnnouncements(String query) {
        filteredAnnouncements.clear();
        
        if (query.isEmpty()) {
            // 如果搜索框为空，显示所有公告
            filteredAnnouncements.addAll(announcements);
        } else {
            // 根据查询字符串过滤公告
            for (AnnouncementItem announcement : announcements) {
                if (announcement.title.contains(query) || 
                    announcement.publisher.contains(query)) {
                    filteredAnnouncements.add(announcement);
                }
            }
        }
        
        // 刷新适配器
        adapter.notifyDataSetChanged();
        
        // 更新底部提示
        if (filteredAnnouncements.isEmpty()) {
            binding.tvEndHint.setVisibility(View.GONE);
        } else {
            binding.tvEndHint.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 公告数据类(简化版，用于列表显示)
     */
    private static class AnnouncementItem {
        String title;
        String publisher;
        String publishTime;
        
        AnnouncementItem(String title, String publisher, String publishTime) {
            this.title = title;
            this.publisher = publisher;
            this.publishTime = publishTime;
        }
    }
    
    /**
     * 公告适配器
     */
    private class AnnouncementAdapter extends RecyclerView.Adapter<AnnouncementAdapter.ViewHolder> {
        
        private List<AnnouncementItem> announcements;
        
        AnnouncementAdapter(List<AnnouncementItem> announcements) {
            this.announcements = announcements;
        }
        
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_announcement, parent, false);
            return new ViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            AnnouncementItem announcement = announcements.get(position);
            holder.tvTitle.setText(announcement.title);
            holder.tvPublisher.setText(announcement.publisher);
            holder.tvPublishTime.setText(announcement.publishTime);
            
            // 查看详情点击事件
            holder.itemView.setOnClickListener(v -> {
                // 创建Announcement对象并设置属性
                com.zz.nondatain.bean.Announcement announcementObj = new com.zz.nondatain.bean.Announcement();
                announcementObj.setId(String.valueOf(position)); // 使用位置作为临时ID
                announcementObj.setTitle(announcement.title);
                announcementObj.setPublisher(announcement.publisher);
                
                // 根据标题设置不同的内容，实际应从API获取
                if ("测试公告".equals(announcement.title)) {
                    announcementObj.setContent("测试公告开始结束");
                } else {
                    announcementObj.setContent("这是一条重要公告，请所有人员认真阅读。\n\n本公告适用于所有非羁押人员，请务必按照要求执行。");
                }
                
                // 解析时间字符串为时间戳 (假设格式为 "yyyy-MM-dd HH:mm")
                try {
                    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm", java.util.Locale.getDefault());
                    java.util.Date date = sdf.parse(announcement.publishTime);
                    if (date != null) {
                        announcementObj.setPublishTime(date.getTime());
                    }
                } catch (Exception e) {
                    // 如果解析失败，使用当前时间
                    announcementObj.setPublishTime(System.currentTimeMillis());
                }
                
                // 使用静态方法启动详情页面
                AnnouncementDetailActivity.start(AnnouncementActivity.this, announcementObj);
            });
        }
        
        @Override
        public int getItemCount() {
            return announcements.size();
        }
        
        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvTitle;
            TextView tvPublisher;
            TextView tvPublishTime;
            
            ViewHolder(View itemView) {
                super(itemView);
                tvTitle = itemView.findViewById(R.id.tv_announcement_title);
                tvPublisher = itemView.findViewById(R.id.tv_publisher);
                tvPublishTime = itemView.findViewById(R.id.tv_publish_time);
            }
        }
    }
} 